package com.flyxteam.commons.collection;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Map的敏捷支持
 * <ul>
 * <li>2017-01-01 | ChrisX | + create</li>
 * </ul>
 * @author ChrisX
 */
public class Maps {

    private static final int DEFAULT_INITIAL_CAPACITY = 16;
    private static final boolean DEFAULT_ORDERED = false;
    
    public static <T> Map<String, Object> create(Object... keyValues) {
        return create(DEFAULT_INITIAL_CAPACITY, DEFAULT_ORDERED, String.class, keyValues);
    }

    public static <T> Map<String, Object> create(int initialCapacity, Object... keyValues) {
        return create(initialCapacity, DEFAULT_ORDERED, String.class, keyValues);
    }
    
    public static <T> Map<String, Object> create(boolean ordered, Object... keyValues) {
        return create(DEFAULT_INITIAL_CAPACITY, ordered, String.class, keyValues);
    }
    
    public static <T> Map<T, Object> create(Class<T> keyType, Object... keyValues) {
        return create(DEFAULT_INITIAL_CAPACITY, DEFAULT_ORDERED, keyType, keyValues);
    }

    @SuppressWarnings("unchecked")
    public static <T> Map<T, Object> create(int initialCapacity, boolean ordered, Class<T> keyType, Object... keyValues) {
        Map<T, Object> map = ordered ? new LinkedHashMap<T, Object>(initialCapacity) : new HashMap<T, Object>(initialCapacity);
        for (int i = 0; i < keyValues.length; i++) {
            map.put((T) keyValues[i], keyValues[i + 1]);
            i++;
        }
        return map;
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void changeKey(Map map, Object oldKey, Object newKey) {
        Object value = map.get(oldKey);
        map.put(newKey, value);
        map.remove(oldKey);
    }
}
